import request from '@/hooks/utils/http'
import { AxiosPromise } from 'axios'
import { RoleVO } from '@/service/RoleService'
import { PostVO } from '@/service/PostService'
import { DeptTreeVO } from '@/service/DeptService'
import { parseStrEmpty } from '@/hooks/utils/curd'

export class UserService {
    /**
     * 查询用户列表
     * @param query
     */
    static listUser = (query: UserQuery): AxiosPromise<UserVO[]> => {
        return request({
            url: '/system/user/list',
            method: 'get',
            params: query
        });
    };

    /**
     * 通过用户ids查询用户
     * @param userIds
     */
    static optionSelect = (userIds: (number | string)[]): AxiosPromise<UserVO[]> => {
        return request({
            url: '/system/user/optionselect?userIds=' + userIds,
            method: 'get'
        });
    };

    /**
     * 获取用户详情
     * @param userId
     */
    static getUser = (userId?: string | number): AxiosPromise<UserInfoVO> => {
        return request({
            url: '/system/user/' + parseStrEmpty(userId),
            method: 'get'
        });
    };

    /**
     * 新增用户
     */
    static addUser = (data: UserForm) => {
        return request({
            url: '/system/user',
            method: 'post',
            data: data
        });
    };

    /**
     * 修改用户
     */
    static updateUser = (data: UserForm) => {
        return request({
            url: '/system/user',
            method: 'put',
            data: data
        });
    };

    /**
     * 删除用户
     * @param userId 用户ID
     */
    static delUser = (userId: Array<string | number> | string | number) => {
        return request({
            url: '/system/user/' + userId,
            method: 'delete'
        });
    };

    /**
     * 用户密码重置
     * @param userId 用户ID
     * @param password 密码
     */
    static resetUserPwd = (userId: string | number, password: string) => {
        const data = {
            userId,
            password
        };
        return request({
            url: '/system/user/resetPwd',
            method: 'put',
            headers: {
                isEncrypt: true,
                repeatSubmit: false
            },
            data: data
        });
    };

    /**
     * 用户状态修改
     * @param userId 用户ID
     * @param status 用户状态
     */
    static changeUserStatus = (userId: number | string, status: string) => {
        const data = {
            userId,
            status
        };
        return request({
            url: '/system/user/changeStatus',
            method: 'put',
            data: data
        });
    };

    /**
     * 查询用户个人信息
     */
    static getUserProfile = (): AxiosPromise<UserInfoVO> => {
        return request({
            url: '/system/user/profile',
            method: 'get'
        });
    };

    /**
     * 修改用户个人信息
     * @param data 用户信息
     */
    static updateUserProfile = (data: UserForm) => {
        return request({
            url: '/system/user/profile',
            method: 'put',
            data: data
        });
    };

    /**
     * 用户密码重置
     * @param oldPassword 旧密码
     * @param newPassword 新密码
     */
    static updateUserPwd = (oldPassword: string, newPassword: string) => {
        const data = {
            oldPassword,
            newPassword
        };
        return request({
            url: '/system/user/profile/updatePwd',
            method: 'put',
            headers: {
                isEncrypt: true,
                repeatSubmit: false
            },
            data: data
        });
    };

    /**
     * 用户头像上传
     * @param data 头像文件
     */
    static uploadAvatar = (data: FormData) => {
        return request({
            url: '/system/user/profile/avatar',
            method: 'post',
            data: data
        });
    };

    /**
     * 查询授权角色
     * @param userId 用户ID
     */
    static getAuthRole = (userId: string | number): AxiosPromise<{ user: UserVO; roles: RoleVO[] }> => {
        return request({
            url: '/system/user/authRole/' + userId,
            method: 'get'
        });
    };

    /**
     * 保存授权角色
     * @param data 用户ID
     */
    static updateAuthRole = (data: { userId: string; roleIds: string }) => {
        return request({
            url: '/system/user/authRole',
            method: 'put',
            params: data
        });
    };

    /**
     * 查询当前部门的所有用户信息
     * @param deptId
     */
    static listUserByDeptId = (deptId: string | number): AxiosPromise<UserVO[]> => {
        return request({
            url: '/system/user/list/dept/' + deptId,
            method: 'get'
        });
    };

    /**
     * 查询部门下拉树结构
     */
    static deptTreeSelect = (): AxiosPromise<DeptTreeVO[]> => {
        return request({
            url: '/system/user/deptTree',
            method: 'get'
        });
    };
}

interface LoginParams {
    userName: string
    password: string
}


/**
 * 用户信息
 */
export interface UserInfo {
    user: UserVO;
    roles: string[];
    permissions: string[];
}

/**
 * 用户查询对象类型
 */
export interface UserQuery extends PageQuery {
    userName?: string;
    nickName?: string;
    phonenumber?: string;
    status?: string;
    deptId?: string | number;
    roleId?: string | number;
    userIds?: string;
}

/**
 * 用户返回对象
 */
export interface UserVO extends BaseEntity {
    userId: string | number;
    tenantId: string;
    deptId: number;
    userName: string;
    nickName: string;
    userType: string;
    email: string;
    phonenumber: string;
    sex: string;
    avatar: string;
    status: string;
    delFlag: string;
    loginIp: string;
    loginDate: string;
    remark: string;
    deptName: string;
    roles: RoleVO[];
    roleIds: any;
    postIds: any;
    roleId: any;
    admin: boolean;
}

/**
 * 用户表单类型
 */
export interface UserForm {
    id?: string;
    userId?: string;
    deptId?: number;
    userName: string;
    nickName?: string;
    password: string;
    phonenumber?: string;
    email?: string;
    sex?: string;
    status: string;
    remark?: string;
    postIds: string[];
    roleIds: string[];
}

export interface UserInfoVO {
    user: UserVO;
    roles: RoleVO[];
    roleIds: string[];
    posts: PostVO[];
    postIds: string[];
    roleGroup: string;
    postGroup: string;
}

export interface ResetPwdForm {
    oldPassword: string;
    newPassword: string;
    confirmPassword: string;
}
